package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.pojo.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

@RestController //@ResponseBody + @Controller的组合注解
@RequestMapping("/emp") //暴露一个公共的路径
@Api(tags = "员工管理接口",description = "员工管理接口描述")
public class EmployeeController {

    /**
     * 注入对象
     */
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 分页查询
     */
    @PostMapping("/findByShopId")
    @ApiOperation(value = "分页查询所有员工接口",notes = "分页查询员工列表")
    public AjaxResult findByShopId(){

        //query ，高级查询的参数  万能键alt+enter
        return AjaxResult.createSuccess(employeeService.findByShopId());
    }
    /**
     * 分页查询
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "分页查询所有员工接口",notes = "分页查询员工列表")
    public PageList<Employee> findPage(@RequestBody EmployeeQuery query){

        //query ，高级查询的参数  万能键alt+enter
        return employeeService.findPage(query);
    }

    /**
     * 查询所有部门经理
     * @RequestMapping(value = "/findAll",method = RequestMethod.GET)
     */
    @GetMapping("/findManager")
    @ApiOperation(value = "查询所有部门经理",notes = "查询所有部门经理")
    public AjaxResult findManager(){

        return AjaxResult.createSuccess(employeeService.findManager());
    }
    /**
     * 查询所有
     * @RequestMapping(value = "/findAll",method = RequestMethod.GET)
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有员工接口",notes = "员工列表")
    public AjaxResult findAll(){

        return AjaxResult.createSuccess(employeeService.findAll());
    }

    /**
     * 新增或者修改，没有id就是新增
     * @param employee
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增修改员工",notes = "没有id就是新增")
    public AjaxResult saveOrUpdate(@RequestBody Employee employee){
        try {
            employeeService.saveOrUpdate(employee);

            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError("操作异常，请联系管理员");
        }

    }

    /**
     * 根据id删除一个对象
     * @return
     * localhost/dept/10
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工",notes = "根据id删除一个对象")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);

            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError("操作异常，请联系管理员");
        }
    }

    public static void main(String[] args) {
        List<Long> longs = Arrays.asList(3L, 8L, 10L, 14L);

        System.out.println(longs.size());
        HashSet<Long> longs1 = new HashSet<>(longs);
        System.out.println(longs1.size());

    }

}
